function onEnter(evt) {
	var keyCode = null;
	
	if( evt.which ) {
		keyCode = evt.which;
	} else if( evt.keyCode ) {
		keyCode = evt.keyCode;
	}
	if( 13 == keyCode ) {
		return true;
	} else {
		return false;
	}
}

function setCookie(name, value)
{
	return $.cookie(name, value, { path: '/' })
}

function checkYouTubeURL(movieURL)
{
	var url = movieURL.toLowerCase();
	if (url.indexOf('youtube.com') >= 0) {
		var segs = movieURL.split('/watch?v=');
		if (segs.length < 2) {
			return null;
		}
		return segs[1].split('&')[0];
	} else if (url.indexOf('looptube.appspot.com') >= 0) {
		var segs = movieURL.split('?');
		segs = segs[0].split('/');
		if (segs.length < 6) {
			return null;
		}
		if (segs[segs.length-3].toLowerCase() != 'loop' || segs[segs.length-2].toLowerCase() != 'youtube') {
			return null;
		}
		return segs[segs.length-1];
	} 
	return null;
}

function getNumHighlightVideos()
{
	var highlightVideos = $.cookie('highlightVideos');
	if (null == highlightVideos) {
		return 0;
	}
	var highlightList = highlightVideos.split(',');
	return highlightList.length;
}

function isVideoHighlighted(videoID)
{
	var highlightVideos = $.cookie('highlightVideos');

	if (null == highlightVideos) {
		return false;
	}
	
	var highlightList = highlightVideos.split(',');
	var i=0;
	for (i=0; i<highlightList.length; i++) {
		if (highlightList[i] == videoID) {
			return true;
		}
	}
	return false;
}

// init vars
var isHighlightLinkWithText = false;

function renderHighlightLink(videoID, addText, nocheck)
{
	if (addText == undefined) {
		addText = isHighlightLinkWithText;
	}
	if (videoID == undefined) {
		videoID = 'all';
	}
	if (nocheck == undefined) {
		nocheck = false;
	}
	if ('all' == videoID) {
		$('.highlightLink').each(function(){
			var id = $(this).attr('id').substr(10);
			renderSingleHighlightLink(id, addText, nocheck);
			
		});
	} else {
		renderSingleHighlightLink(videoID, addText, nocheck);
	}
}
	
function renderSingleHighlightLink(videoID, addText, nocheck)
{
	if (nocheck) {
		renderSingleHighlightLinkAux(videoID, addText, nocheck)
	} else {
		$.getJSON('/channel/fetch?videoID='+videoID, function(data) {
			if (data.videoID) {
				renderSingleHighlightLinkAux(videoID, addText, nocheck)
			} else {
				if (addText) {
					$('#highlight-'+videoID).html('<b>'+gettext('Video not loopable')+'</b>');
				}
			}
		});
	}
}

function renderSingleHighlightLinkAux(videoID, addText, nocheck)
{
	var el = $('#highlight-'+videoID);
	if (isVideoHighlighted(videoID)) {
		el.html('<a title="'+gettext('Remove from highlight videos')+'" href="javascript:;" onclick="javascript:removeHighlightVideo(\''+videoID+'\');">'
			+ '<img alt="'+gettext('Remove from highlight videos')+'" src="/static/images/star-on.jpg"/>'
			+ ((addText) ? ' <b>'+gettext('Remove from highlight videos')+'</b>' : '')
			+ '</a>');
	} else {
		el.html('<a title="'+gettext('Add to highlight videos')+'" href="javascript:;" onclick="javascript:addHighlightVideo(\''+videoID+'\');">'
			+ '<img alt="'+gettext('Add to highlight videos')+'" src="/static/images/star-off.jpg"/>'
			+ ((addText) ? ' <b>'+gettext('Add to highlight videos')+'</b>' : '')
			+ '</a>');
	}
}

function addHighlightVideo(videoID)
{
	if (!videoID) {
		return false;
	}
	var highlightVideos = $.cookie('highlightVideos');

	if (getNumHighlightVideos() >= 20) {
		alert(gettext('Maximum number of highlight videos (20) reached, please remove some highlighted videos before adding new videos!'));
		return false;
	}
	if (isVideoHighlighted(videoID)) {
		alert(gettext('This video is already highlighted!'));
		return false;
	}
	
	$.getJSON('/channel/fetch?videoID='+videoID, function(data) {
		if (data.videoID) {
			if (null == highlightVideos || '' == highlightVideos) {
				setCookie('highlightVideos', data.videoID);
			} else if (!isVideoHighlighted(data.videoID)) {
				setCookie('highlightVideos', highlightVideos+','+data.videoID);
			}
		
			renderHighlightLink(data.videoID, isHighlightLinkWithText, true);
			loadHighlightVideosSidebar();
			return true;
		} else {
			alert(gettext('This video is not loopable!'));
			return false; 
		}
	});

}

function removeHighlightVideo(videoID)
{
	if (!videoID) {
		return false;
	}
	var highlightVideos = $.cookie('highlightVideos');

	if (isVideoHighlighted(videoID)) {
		if (highlightVideos.indexOf(videoID) == 0) {
			setCookie('highlightVideos', highlightVideos.replace(videoID, ''));
			if ('' == $.cookie('highlightVideos')) {
				setCookie('highlightVideos', null);
			}
		} else {
			setCookie('highlightVideos', highlightVideos.replace(','+videoID, ''));
		}
	}

	renderHighlightLink(videoID, isHighlightLinkWithText, true);
	loadHighlightVideosSidebar();
	return;
}

/**
*
*  Javascript trim, ltrim, rtrim
*  http://www.webtoolkit.info/
*
*
**/

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

/* trim end */